package com.myjob.handlerImpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.myjob.common.DubboConfig;
import com.myjob.config.Constants;
import com.myjob.config.MjServiceName;
import com.myjob.entity.Company;
import com.myjob.handler.testHandler;
import com.myjob.mapper.CompanyMapper;
import com.myjob.response.Result;
import com.myjob.utils.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author Chen Yuan
 * @create 2021-01-27  18:55
 */
@Slf4j
@Component
@Service(group = MjServiceName.COMPANY,
		retries = DubboConfig.RETRIES,
		timeout = DubboConfig.TIME_OUT,
		version = DubboConfig.VERSION)
public class TestHandlerImpl implements testHandler {

	@Autowired
	private CompanyMapper companyMapper;

	@Override
	public Result selectCompanies(Integer currentPage, Integer pageSize, String companyName, String financingSituation, String companySize, String enterprisesType, String memberLevel, String companyStatus) {
		log.info("[Dubbo] - 被远程调用 -> TestHandler -> selectCompanies");

		Result result = null;
		QueryWrapper<Company> wrapper = new QueryWrapper<>();
		if (UtilValidate.isNotEmpty(companyName)){
			wrapper.like("company_name", companyName);
		}
		if (UtilValidate.isNotEmpty(financingSituation)){
			wrapper.eq("financing_situation",financingSituation);
		}
		if (UtilValidate.isNotEmpty(companySize)){
			wrapper.eq("company_size", companySize);
		}
		if (UtilValidate.isNotEmpty(enterprisesType)){
			wrapper.eq("enterprises_type",enterprisesType);
		}
		if (UtilValidate.isNotEmpty(memberLevel)){
			wrapper.eq("member_level", memberLevel);
		}
		if (UtilValidate.isNotEmpty(companyStatus)){
			wrapper.eq("company_status", companyStatus);
		}

		Page<Company> page = null;
		if (UtilValidate.isEmpty(pageSize)){
			page = new Page<>(currentPage, Constants.getPageSize());
		}else {
			page = new Page<>(currentPage, pageSize);
		}
		IPage<Company> companies = companyMapper.selectPage(page, wrapper);
		result = Result.succeed(companies);
		return result;
	}
}
